package cn.kgc.service.impl;

import cn.kgc.dto.PmsBrandAddDTO;
import cn.kgc.dto.PmsBrandUpdateDTO;
import cn.kgc.entity.PmsCategory;
import cn.kgc.entity.PmsCategoryBrandRelation;
import cn.kgc.mapper.PmsCategoryBrandRelationMapper;
import cn.kgc.mapper.PmsCategoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.mapper.PmsBrandMapper;
import cn.kgc.entity.PmsBrand;
import cn.kgc.service.PmsBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 品牌(PmsBrand)表服务实现类
 *
 * @author 石浩杰
 * @since 2022-11-27 13:39:19
 */
@Slf4j
@Service("pmsBrandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {
    @Resource
    private PmsBrandMapper pmsBrandMapper;

    @Resource
    private PmsCategoryBrandRelationMapper pmsCategoryBrandRelationMapper;

    @Resource
    private PmsCategoryMapper pmsCategoryMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateBrandAndRelation(PmsBrandUpdateDTO pmsBrandUpdateDTO) {
        //更新品牌的数据
        PmsBrand pmsBrand = this.pmsBrandMapper.selectById(pmsBrandUpdateDTO.getId());
        BeanUtils.copyProperties(pmsBrandUpdateDTO, pmsBrand);
        pmsBrandMapper.updateById(pmsBrand);
        //删除原有的品牌与分类之间的关系
        LambdaQueryWrapper<PmsCategoryBrandRelation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsCategoryBrandRelation::getBrandId,pmsBrand.getId());
        pmsCategoryBrandRelationMapper.delete(lambdaQueryWrapper);
        //新增品牌与分类之间的关系
        List<Long> categoryIds=pmsBrandUpdateDTO.getCategoryIds();
        insertBrandAndRelation(pmsBrand, categoryIds);
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteBrandById(Long brandId) {
        //删除品牌
        pmsBrandMapper.deleteById(brandId);
        //删除品牌和分类的关联关系
        LambdaQueryWrapper<PmsCategoryBrandRelation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsCategoryBrandRelation::getBrandId,brandId);
        pmsCategoryBrandRelationMapper.delete(lambdaQueryWrapper);
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean saveBrandAndRelation(PmsBrandAddDTO pmsBrandAddDTO) {
        //新增的品牌的数据
        PmsBrand pmsBrand=new PmsBrand();
        BeanUtils.copyProperties(pmsBrandAddDTO,pmsBrand);
        log.info("新增的品牌的数据:{}",pmsBrand);
        pmsBrandMapper.insert(pmsBrand);
        //新增品牌和分类的关系数据
        List<Long> categoryIds=pmsBrandAddDTO.getCategoryIds();
        insertBrandAndRelation(pmsBrand, categoryIds);
        return true;
    }
    private void insertBrandAndRelation(PmsBrand pmsBrand, List<Long> categoryIds) {
        for (Long categoryId : categoryIds) {
            PmsCategoryBrandRelation pmsCategoryBrandRelation=new PmsCategoryBrandRelation();
            pmsCategoryBrandRelation.setBrandId(pmsBrand.getId());
            pmsCategoryBrandRelation.setCatelogId(categoryId);
            pmsCategoryBrandRelation.setBrandName(pmsBrand.getName());
            PmsCategory pmsCategory=pmsCategoryMapper.selectById(categoryId);
            if (pmsCategory!=null){
                pmsCategoryBrandRelation.setCatelogName(pmsCategory.getName());
                log.info("新增品牌和分类的关系数据:{}",pmsCategoryBrandRelation);
                pmsCategoryBrandRelationMapper.insert(pmsCategoryBrandRelation);
            }
        }
    }
}
